<?php

namespace App\Admin\Renderables;

use App\Admin\Actions\Delete;
use App\Logics\Logic;
use App\Models\AdminUser;
use App\Models\DyBaseInfo;
use App\Models\DyFzdx;
use App\Models\DyXfzdy;
use App\Models\Role;
use App\Models\SpecialNode;
use App\Models\User;
use App\Policies\Admin\AdminPolicy;
use Dcat\Admin\Admin;
use Dcat\Admin\Grid;
use Dcat\Admin\Grid\LazyRenderable;
use Dcat\Admin\Widgets\Tooltip;

class DyFzdxRenderTable extends LazyRenderable
{
    public function grid(): Grid
    {
        $admin_user = AdminUser::find(Admin::user()->id);
        $sql = isset($this->payload['sql']) ? $this->payload['sql'] : '';
        $sql = $sql ? encrypt_decode($sql) : '';
        $builder = Logic::getBaseQuery(DyFzdx::query(), $admin_user);
        if($sql){
            $builder->whereRaw($sql);
        }

        $grid = Grid::make($builder, function (Grid $grid) {
            $grid->column('name')->display(function($v) {
                if ($this->is_error) {
                    Tooltip::make('.bs-'.$this->id)->title(implode("<br/>", json_decode($this->errors, true)))->background('#ea5455')->maxWidth(600);
                    return '<span style="color:#304ffe; cursor: pointer" class="text-danger bs-'.$this->id.'">'.$v.'</span>';
                } else {
                    return $v;
                }
            });
            $grid->column('zjhm');
            $grid->column('dzb',' 所属党支部');
            $grid->column('gzgw', '工作岗位');
            $grid->column('sfz', '身份证号');
            $grid->column('age', '年龄');
            $grid->column('sex', '性别');
            $grid->column('mz', '民族');
            $grid->column('jrsj', '确定发展对象时间');
            $grid->disableActions();
            $grid->setDialogFormDimensions('700px', '750px');
            $grid->filter(function (Grid\Filter $filter) {
                $filter->panel()->expand();
                $filter->where('keyword', function($query) {
                    $query->where(function($q) {
                        $q->where('name', 'like', "%{$this->input}%")
                            ->orWhere('sfz', 'like', "%{$this->input}%");
                    });
                }, '关键字')->placeholder('请输入姓名/身份证号')->width(3);
                $filter->like('dzzmc')->width(3);
                $filter->equal('gzgw')->select(array_combine(dy_base_gzgw(), dy_base_gzgw()))->width(3);
                $filter->between('age')->width(3);
                $filter->equal('is_error', '异常数据')->select([1 => '异常'])->width(3);
            });
            $grid->disableCreateButton();
        });
        $grid->disableActions()->disableFilter()->disableCreateButton()->disableRowSelector();
        $grid->paginate(10);
        return $grid;
    }
}